package com.yphone.ui.login.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.zzkj.yunphone.R;
import com.yphone.tools.Constants;
import com.yphone.tools.ToastUtil;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.login.bean.AccountInfo;
import com.yphone.ui.login.contract.LoginActivityContract;
import com.yphone.ui.login.presenter.LoginActivityPresenter;
import com.yphone.ui.login.view.PwdView;
import com.yphone.ui.login.view.TelView;
import com.yphone.utils.AccountUtils;
import com.yphone.utils.NetworkUtils;
import com.yphone.utils.RegexUtils;
import com.yphone.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

public class LoginActivity extends AppCompatActivity implements LoginActivityContract.IView, OnClickListener {
    private static final String PARAM_NEXTSTEP = "nextStep";
    /* access modifiers changed from: private */
    public TelView mEdtNumber;
    /* access modifiers changed from: private */
    public PwdView mEdtPassword;
    /* access modifiers changed from: private */
    public LoginActivityContract.IPresenter mIPresenter;
    private ImageView mImgBack;
    private ImageView mImgNumberClean;
    private View mLayoutUserName;
    private TextView mTxtForgetPassWord;
    private TextView mTxtLoginBtn;
    private TextView mTxtLoginRemind;
    private TextView mTxtRegister;
    /* access modifiers changed from: private */
//    public QQModel qqModel;

    public static void toLoginActivity(Context context) {
        toLoginActivity(context, 0);
    }

    public static void toLoginActivity(Context context, int nextStep) {
        Intent intent = new Intent(context, LoginActivity.class);
        intent.putExtra(PARAM_NEXTSTEP, nextStep);
        context.startActivity(intent);
    }

    /* access modifiers changed from: protected */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView((int) R.layout.activity_login);
        EventBus.getDefault().register(this);
        initDataBeforView();
        initView();
        initData();
        initListener();
    }

    public void initDataBeforView() {
    }

    public void initView() {
        this.mImgBack = (ImageView) findViewById(R.id.activity_login_img_back);
        this.mTxtLoginRemind = (TextView) findViewById(R.id.activity_login_txt_login_remind);
        this.mTxtLoginRemind.setVisibility(View.GONE);
        this.mEdtNumber = (TelView) findViewById(R.id.activity_login_edt_number);
        this.mImgNumberClean = (ImageView) findViewById(R.id.activity_login_img_number_clean);
        this.mEdtPassword = (PwdView) findViewById(R.id.activity_login_edt_password);
        this.mTxtLoginBtn = (TextView) findViewById(R.id.activity_login_txt_login);
        this.mTxtRegister = (TextView) findViewById(R.id.activity_login_txt_register);
        this.mTxtForgetPassWord = (TextView) findViewById(R.id.activity_login_txt_forget_password);
        this.mLayoutUserName = findViewById(R.id.activity_login_layout_username);
    }

    public void initData() {
        new LoginActivityPresenter(this);
        AccountInfo accountInfo = AccountUtils.getInstance().getCurrentAccount();
        if (!TextUtils.isEmpty(accountInfo.userName)) {
            this.mEdtNumber.setText(accountInfo.userName);
        }
        if (!TextUtils.isEmpty(accountInfo.passWord)) {
            this.mEdtPassword.setText(accountInfo.passWord);
        }
    }

    public void initListener() {
        this.mImgBack.setOnClickListener(this);
        this.mImgNumberClean.setOnClickListener(this);
        this.mTxtLoginBtn.setOnClickListener(this);
        this.mTxtRegister.setOnClickListener(this);
        this.mTxtForgetPassWord.setOnClickListener(this);
    }

    public void onClick(View view) {
        if (view.getId() == this.mImgBack.getId()) {
            finish();
        } else if (view.getId() == this.mImgNumberClean.getId()) {
            this.mEdtNumber.setText("");
        } else if (view.getId() == this.mTxtLoginBtn.getId()) {
            submit();
        } else if (view.getId() == this.mTxtRegister.getId()) {
            RegisterActivity.toRegisterActivity(this);
        } else if (view.getId() == this.mTxtForgetPassWord.getId()) {
            ForgetPasswordActivity.toForgetPasswordActivity(this);
        } else if (view.getId() == R.id.activity_login_input_more_drop) {
//            AccountListPop.showPopupWindow(this, this.mLayoutUserName, new AccountListCallBack() {
//                public void onItemChoose(AccountInfo accountInfo) {
//                    LoginActivity.this.mEdtNumber.setText(accountInfo.userName);
//                    LoginActivity.this.mEdtPassword.setText(accountInfo.passWord);
//                }
//
//                public void onItemDelete(AccountInfo accountInfo) {
//                    if (LoginActivity.this.mEdtNumber.getText().toString().equals(accountInfo.userName)) {
//                        LoginActivity.this.mEdtNumber.setText("");
//                        LoginActivity.this.mEdtPassword.setText("");
//                    }
//                    if (AccountUtils.getInstance().getCurrentAccount().userName.equals(accountInfo.userName)) {
//                        AccountUtils.getInstance().saveCurrentAccount(null);
//                    }
//                }
//            });
        }
    }

    /* access modifiers changed from: private */
    public void loginFailedTip() {
        CommSmallLoadingDialog.dismissDialog();
        ToastUtils.showLong((CharSequence) "登录失败");
    }

    public void submit() {
        String account = this.mEdtNumber.getText().toString();
        if (TextUtils.isEmpty(account)) {
            ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_phone_empty));
        } else if (account.length() != 11) {
            ToastUtils.showLong((CharSequence) getString(R.string.phone_format));
        } else {
            String password = this.mEdtPassword.getText().toString();
            if (TextUtils.isEmpty(password)) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_pwd_empty));
            } else if (password.length() < 6) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.login_pwd_six));
            } else if (!RegexUtils.isMatch(Constants.PWD_MATCHING, password)) {
                ToastUtils.showLong((CharSequence) getResources().getString(R.string.mmgssryw));
            } else if (!NetworkUtils.isConnected()) {
                ToastUtils.showLong((CharSequence) getString(R.string.no_net));
            } else {
                CommSmallLoadingDialog.showDialog(this, getString(R.string.request_ing));
                this.mIPresenter.requestLoginEvent(account, password);
            }
        }
    }

    public void setPresenter(LoginActivityContract.IPresenter p) {
        this.mIPresenter = p;
    }

    /* access modifiers changed from: protected */
    public void onDestroy() {
        super.onDestroy();
        this.mIPresenter.destory();
        EventBus.getDefault().unregister(this);
    }

    public void finish() {
        if (getNextStep() == 2) {
//            IntentUtils.toMainActivity(this, 0);
        }
        super.finish();
    }

    /* access modifiers changed from: protected */
    public void onResume() {
        super.onResume();
//        CommSmallLoadingDialog.dismissDialog();
    }

    public Context getContext() {
        return this;
    }

    public int getNextStep() {
        return getIntent().getIntExtra(PARAM_NEXTSTEP, 0);
    }

    /* access modifiers changed from: protected */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (this.qqModel != null) {
//            this.qqModel.onActivityResult(requestCode, resultCode, data);
//        }
    }
    @Subscribe
    public void onEventMainThread(MyEvent.LoginSuccessEvent event) {
        ToastUtil.showToast(this,"登录成功");
        finish();
    }
//
//    public void onEventMainThread(GetUserInfoSuccessEvent event) {
//        finish();
//    }
//
//    public void onEventMainThread(PhoneBingSuccessEvent event) {
//        finish();
//    }
}